package fr.paulmasson.jfreex.db.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import fr.paulmasson.jfreex.db.bo.Bo;

public class Dao {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void printTable(List<Object> data) {
		// Find out what the maximum number of columns is in any row
		
		
		if (data.size() > 0 && data.get(0).getClass().getGenericSuperclass() != Bo.class) {
			System.err.println("ERROR: can't display an non Bo list");
			return;
		}

		Bo first = (Bo) data.get(0);
		Class c = first.getClass();
		
		data.remove(0);
		
		try {
				
				String[] cols = (String[]) c.getMethod("getColumns",(Class[])null).invoke(null);
				int[] lengths = new int[cols.length];
				
				for (int i=0; i<cols.length; i++) {
					lengths[i] = Math.max(cols[i].length(), lengths[i]);
				}
				
				for (Object o: data) {
					for (int j=0; j<cols.length; j++) {
						String value = ((String)((Bo)o).getColumnValue(j));
						if (null == value)
							value = "NULL";
						lengths[j] = Math.max( value.length(), lengths[j]);	
					}			
				}
				
	//			 Generate a format string for each column
				String[] formats = new String[lengths.length];
				StringBuffer line = new StringBuffer();
				StringBuffer empty_line = new StringBuffer();
				StringBuffer full_line = new StringBuffer();
				StringBuffer last_line = new StringBuffer();
				int total_length = 0;
				for (int i = 0; i < lengths.length; i++) {
					formats[i] = (i == 0 ? "| " : "") 
							+ "%-" + lengths[i] + "s"
							+ (i + 1 == lengths.length ? " |\n" : " | ");
					total_length += lengths[i] + 3;
					line.append(i == 0 ? "|-" : "");
					full_line.append(i == 0 ? " _" : "");
					last_line.append(i == 0 ? "'-" : "");
					empty_line.append(i == 0 ? "|-" : "");
					for (int j=0; j<lengths[i]; j++) {
						line.append("-");
						full_line.append("_");
						last_line.append("-");
						empty_line.append("-");
					}
					line.append(i + 1 == lengths.length ? "-|\n" : "-|-");
					full_line.append(i + 1 == lengths.length ? "_\n" : "___");
					last_line.append(i + 1 == lengths.length ? "-'\n" : "---");
					empty_line.append(i + 1 == lengths.length ? "-|\n" : "---");
				}
		
				System.out.print(full_line.toString());
				System.out.printf("| %-"+(total_length-2)+"s|\n",(String)c.getMethod("getTableName",(Class[])null).invoke(null));
				System.out.print(empty_line.toString());		
				
				for (int j=0; j<cols.length; j++) {
					System.out.printf(formats[j], cols[j]);
				}	
				
				System.out.print(line.toString());
				
				for (Object o: data) {
					for (int j=0; j<cols.length; j++) {
						System.out.printf(formats[j], ((String)((Bo)o).getColumnValue(j)));
						
					}
				}
				System.out.print(last_line.toString());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	
	
}
